package com.yd.spark.demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;

import com.google.common.io.Files;

public class UnicodeUtils {

	public static void main(String[] args) throws IOException {
//		String log = "\u73b0\u4ee3\u65b0\u80dc\u8fbe(\u8fdb\u53e3)";
		File logFile = new File("dispatch_detail_info.txt");
		String line = Files.readFirstLine(logFile, Charset.forName("UTF-8"));
		System.out.println(line);
		System.out.println(convertUnicode(line));
		System.out.println(convertUnicode_2(line));
		
		line = "\\u73b0\\u4ee3\\u65b0\\u80dc\\u8fbe";
		System.out.println(line);
		System.out.println(convertUnicode(line));
		System.out.println(convertUnicode_2(line));
		Path path = logFile.toPath();
		BufferedReader in = java.nio.file.Files.newBufferedReader(path, Charsets.UTF_8);
		while ((line = in.readLine()) != null) {
			line = convertUnicode(line);
			String[] fields = line.split(" ", -1);
			if(fields.length != 31)
				System.err.println(line);
		}
		IOUtils.closeQuietly(in);
		
//		List<String> lines = FileUtils.readLines(logFile);
//		Files.newBufferedReader(Paths.get(filename), Charsets.UTF_8);
//		com.google.common.io.Files.readLines(file, charset, callback)
//		CharSource charsource = Files.asCharSource(new File(filename),Charsets.UTF_8);
	}

	public static String convertUnicode(String utfString){  
	    StringBuilder sb = new StringBuilder();  
	    int i = -1;  
	    int pos = 0;  
	      
	    while((i=utfString.indexOf("\\u", pos)) != -1){  
	        sb.append(utfString.substring(pos, i));  
	        if(i+5 < utfString.length()){  
	            pos = i+6;  
	            sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));  
	        }  
	    }  
	    sb.append(utfString.substring(pos));  
	    return sb.toString();  
	}  
	
	public static String convertUnicode(byte[] inputMessage){  
	    StringBuilder sb = new StringBuilder();  
	    int i = -1;  
	    int pos = 0;  
	    String utfString = new String(inputMessage);
	    while((i=utfString.indexOf("\\u", pos)) != -1){  
	        sb.append(utfString.substring(pos, i));  
	        if(i+5 < utfString.length()){  
	            pos = i+6;  
	            sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));  
	        }  
	    }  
	    sb.append(utfString.substring(pos));  
	    return sb.toString();  
	} 
	
	public static String convertUnicode_2(String ori){
        char aChar;
        int len = ori.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len;) {
            aChar = ori.charAt(x++);
            if (aChar == '\\') {
                aChar = ori.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = ori.charAt(x++);
                        switch (aChar) {
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                            value = (value << 4) + aChar - '0';
                            break;
                        case 'a':
                        case 'b':
                        case 'c':
                        case 'd':
                        case 'e':
                        case 'f':
                            value = (value << 4) + 10 + aChar - 'a';
                            break;
                        case 'A':
                        case 'B':
                        case 'C':
                        case 'D':
                        case 'E':
                        case 'F':
                            value = (value << 4) + 10 + aChar - 'A';
                            break;
                        default:
                            throw new IllegalArgumentException(
                                    "Malformed   \\uxxxx   encoding.");
                        }
                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
 
        }
        return outBuffer.toString();
	}
	
//	ufferedReader in = Files.newBufferedReader(Paths.get(filename), Charsets.UTF_8);
//	String line = in.readLine();
//	while ((line = in.readLine()) != null) {
//		String[] fields = line.split(LogDelimiter.TAB_SEP, -1);
//		blackIPSet.add(fields[0]);
//	}
//	IOUtils.closeQuietly(in);
//	com.google.common.io.Files.readLines(file, charset, callback)
//	FileUtils.lineIterator(filename, "UTF-8");
//	CharSource charsource = Files.asCharSource(new File(filename),Charsets.UTF_8);
//	charsource.readLines(new LineProcessor<Integer>() {
//		private int lineCount=0;
//		@Override
//		public Integer getResult() {
//			return lineCount;
//		}
//		@Override
//		public boolean processLine(String line) throws IOException {
//			if(!StringUtils.isBlank(line)){
//				String[] fields = line.split(LogDelimiter.TAB_SEP, -1);
//				blackIPSet.add(fields[0]);
//			}
//			lineCount++;
//			return true;
//		}					
//	});
}
